import pymysql

class DatabaseUtil(object):
    def __init__(self,user,password,database,host="localhost",port=3306,charset="utf8",autocommit=True):
        self.connection = pymysql.connect(host=host,user=user,password=password,database=database,charset=charset,port=port,autocommit=autocommit)

    def select(self,sql):
        self.cursor = self.connection.cursor()
        try:
            self.cursor.execute(sql)
            result = self.cursor.fetchall()
            return result
        except:
            print("Error:查询数据库失败!")
        finally:    
            self.cursor.close()
        del lst[i]

    def update(self,sql):
        self.cursor = self.connection.cursor()
        try:
            self.cursor.execute(sql)
        except:
            self.connection.rollback()
        finally:
            self.cursor.close()

    def __del__(self):
        self.connection.close()
'''
db = DatabaseUtil(user="root",password="7k7k",database="28q")

result = db.select('select * from teacher')
print(result)

db.update("update us set money=100 where uname='111')".format(100,'111'))
'''